% MATLAB Stochastic Dominance with FSD and SSD
clear; clc; rng(42);

% Simulation Parameters
N = 10000;   % Number of Monte Carlo Simulations
T = 50;      % Time Periods
mu = 0.05;   % Expected market return

% Define Risk Scenarios
sigma_high = 0.4;  % High risk (Extreme Uncertainty)
sigma_low = 0.1;   % Low risk (Mild Uncertainty)

% Cognitive Dissonance and Extrapolation Parameters
lambda_LA = 2.0;  % Loss aversion coefficient
alpha = 0.88;     % Risk preference exponent
theta = 0.5;      % Extrapolation coefficient

% Prospect Theory Probability Weighting
gamma_gain = 0.75;  
gamma_loss = 0.65;  

% Market Return Generation
returns_high = mu + sigma_high * randn(N, T);
returns_low = mu + sigma_low * randn(N, T);

% Probability Weighting Function
w_p = @(p, gamma) (p.^gamma) ./ (p.^gamma + (1 - p).^gamma);

% Loss Aversion Utility Function
U = @(x) (x >= 0) .* (x .^ alpha) - (x < 0) .* lambda_LA .* ((-x) .^ alpha);

% Crisis-Specific Belief Updating (Forcing Cognitive Dissonance)
belief_weighting = @(sigma) 1 ./ (1 + exp(-10 * (sigma - 0.2)));  
% This makes investors rely **more on prior beliefs** when uncertainty is extreme.

% Apply Probability Weighting
prob_weighted_returns_high = belief_weighting(sigma_high) .* w_p(normcdf(returns_high), gamma_gain) - (1 - belief_weighting(sigma_high)) .* w_p(1 - normcdf(returns_high), gamma_loss);
prob_weighted_returns_low = belief_weighting(sigma_low) .* w_p(normcdf(returns_low), gamma_gain) - (1 - belief_weighting(sigma_low)) .* w_p(1 - normcdf(returns_low), gamma_loss);

% Wealth Evolution with Cognitive Dissonance vs. Extrapolative Beliefs
wealth_cognitive_high = cumprod(1 + U(prob_weighted_returns_high), 2);
wealth_extrapolative_high = cumprod(1 + U(returns_high), 2);

wealth_cognitive_low = cumprod(1 + U(prob_weighted_returns_low), 2);
wealth_extrapolative_low = cumprod(1 + U(returns_low), 2);

% Extract Final Wealth Distributions
final_wealth_cognitive_high = wealth_cognitive_high(:, end);
final_wealth_extrapolative_high = wealth_extrapolative_high(:, end);

final_wealth_cognitive_low = wealth_cognitive_low(:, end);
final_wealth_extrapolative_low = wealth_extrapolative_low(:, end);

% === First-Order Stochastic Dominance (FSD) ===
[x_high, F_cognitive_high] = ecdf(final_wealth_cognitive_high);
[x_high, F_extrapolative_high] = ecdf(final_wealth_extrapolative_high);

[x_low, F_cognitive_low] = ecdf(final_wealth_cognitive_low);
[x_low, F_extrapolative_low] = ecdf(final_wealth_extrapolative_low);

fsd_high = all(F_cognitive_high <= F_extrapolative_high) && any(F_cognitive_high < F_extrapolative_high);
fsd_low = all(F_extrapolative_low <= F_cognitive_low) && any(F_extrapolative_low < F_cognitive_low);

% === Second-Order Stochastic Dominance (SSD) ===
ssd_high = all(cumtrapz(x_high, F_cognitive_high) <= cumtrapz(x_high, F_extrapolative_high));
ssd_low = all(cumtrapz(x_low, F_extrapolative_low) <= cumtrapz(x_low, F_cognitive_low));

% === Display Results ===
disp('--- Stochastic Dominance Results ---');
disp('High Volatility (Extreme Uncertainty):');
if fsd_high
    disp('Cognitive Dissonance **First-Order Dominates** Extrapolative Beliefs.');
elseif ssd_high
    disp('Cognitive Dissonance **Second-Order Dominates** Extrapolative Beliefs.');
else
    disp('No clear stochastic dominance under high volatility.');
end

disp('Low Volatility (Mild Uncertainty):');
if fsd_low
    disp('Extrapolative Beliefs **First-Order Dominates** Cognitive Dissonance.');
elseif ssd_low
    disp('Extrapolative Beliefs **Second-Order Dominates** Cognitive Dissonance.');
else
    disp('No clear stochastic dominance under low volatility.');
end

% === Plot Adjusted CDFs ===
figure;
subplot(1,2,1); hold on;
plot(x_high, F_cognitive_high, 'r', 'LineWidth', 2);
plot(x_high, F_extrapolative_high, 'b', 'LineWidth', 2);
legend('Cognitive Dissonance', 'Extrapolative Beliefs', 'Location', 'NorthWest');
xlabel('Final Wealth'); ylabel('Cumulative Probability');
title('High Volatility (Extreme Uncertainty)');
grid on;

subplot(1,2,2); hold on;
plot(x_low, F_extrapolative_low, 'b', 'LineWidth', 2);
plot(x_low, F_cognitive_low, 'r', 'LineWidth', 2);
legend('Extrapolative Beliefs', 'Cognitive Dissonance', 'Location', 'NorthWest');
xlabel('Final Wealth'); ylabel('Cumulative Probability');
title('Low Volatility (Mild Uncertainty)');
grid on;
